package cn.bdqn.controller;

import cn.bdqn.pojo.SysJobPo;
import cn.bdqn.service.SysJobPoService;
import cn.bdqn.task.CronTaskRegistrar;
import cn.bdqn.thread.SchedulingRunnable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Controller
public class JobController {

    @Resource
    private CronTaskRegistrar cronTaskRegistrar;
    @Resource
    private SysJobPoService sysJobPoService;

    /**
     * 跳转到主页
     */
    @GetMapping("/index")
    public String index(Model model){
        List<SysJobPo> jobs = sysJobPoService.getAll();
        model.addAttribute("jobs",jobs);
        return "index";
    }

    /**
     * 跳转到添加任务页面
     */
    @GetMapping("/job/add")
    public String add(){
        return "add";
    }

    /**
     * 添加定时任务
     */
    @PostMapping("/job/add")
    public String add(SysJobPo sysJob){
        int row = sysJobPoService.add(sysJob);
        if (row != 1)
            return "redirect:/job/add";
        else {
            if (sysJob.getJobStatus()==1) {
                SchedulingRunnable task = new SchedulingRunnable(sysJob.getBeanName(), sysJob.getMethodName(), sysJob.getMethodParams());
                cronTaskRegistrar.addCronTask(task, sysJob.getCronExpression());
            }
        }
        return "redirect:/index";
    }

    /**
     * 删除定时任务
     */
    @GetMapping("/job/del")
    public String del(Integer jobId){
        SysJobPo sysJob = sysJobPoService.getById(jobId);
        int row = sysJobPoService.del(jobId);
        if(row > 0){
            SchedulingRunnable task = new SchedulingRunnable(sysJob.getBeanName(), sysJob.getMethodName(), sysJob.getMethodParams());
            cronTaskRegistrar.removeCronTask(task);
        }
        return "redirect:/index";
    }

    /**
     * 跳转到修改定任务页面
     */
    @GetMapping("/job/edit")
    public String edit(Integer jobId,Model model){
        SysJobPo sysJob = sysJobPoService.getById(jobId);
        model.addAttribute("sysJob",sysJob);
        return "edit";
    }

    /**
     * 修改定时任务，可以修改具体的内容，也可以切换定时任务的状态
     */
    @PostMapping("/job/edit")
    public String edit(SysJobPo sysJobPo,Model model) {
        SysJobPo oldSysJob = sysJobPoService.getById(sysJobPo.getJobId());
        sysJobPo.setUpdateTime(new Date());
        int row = sysJobPoService.edit(sysJobPo);
        if (row < 1){
            model.addAttribute("sysJob", sysJobPo);
            return "edit";
        }else{
            //如果原任务是启动中，则先移除原来的任务
            if(oldSysJob.getJobStatus()==1){
                SchedulingRunnable task = new SchedulingRunnable(oldSysJob.getBeanName(), oldSysJob.getMethodName(), oldSysJob.getMethodParams());
                cronTaskRegistrar.removeCronTask(task);
            }
            if(sysJobPo.getJobStatus() == 1){
                SchedulingRunnable task = new SchedulingRunnable(sysJobPo.getBeanName(), sysJobPo.getMethodName(), sysJobPo.getMethodParams());
                cronTaskRegistrar.addCronTask(task, sysJobPo.getCronExpression());
            }
            return "redirect:/index";
        }
    }


}
